package ac.uk.qmul.gemom.scenario;


import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

/*
 * A class used for counting message rates 
 * 
 * COPYRIGHT DISCLAIMER
 * Synopsis: All the source codes presented in this document are intellectual
 * properties of Queen Mary, University of London.
 * 
 * @year 2010
 * @author Peng Jiang, John Bigham, Jiayi Wu Jinfu Wang------------QMUL
 * @version 1.0 
 */

public class ModeDialog extends JDialog implements ActionListener
{
    private JTextField ns = new JTextField( 10 );
    private JTextField th = new JTextField( 10 );
    private JButton subButton;
    private JButton pubButton;
    private JButton fmButton;
    private JFrame parent;
    private int mode;
    
    public ModeDialog( JFrame inParent )
    {
        super( inParent );
     
        parent = inParent;
        
        buildUI();
    }
    
    private void buildUI()
    {
        setAlwaysOnTop( true );
        setTitle( "Select Job Mode" );
        
        pubButton = new JButton( "AutoPublisher" );
        pubButton.addActionListener( this );
        subButton = new JButton( "AutoSubscriber" );
        subButton.addActionListener( this );
        
        fmButton = new JButton( "Fault Manager" );
        fmButton.addActionListener( this );

        JPanel stuff = new JPanel( new GridBagLayout() );

        GridBagConstraints gbc = new GridBagConstraints();
        
        JPanel buttons = new JPanel( new FlowLayout( FlowLayout.CENTER, 0, 1 ) );
        buttons.add( pubButton );
        buttons.add( subButton );
        buttons.add( fmButton );

        gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.CENTER;
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.insets.top = 6;
        stuff.add( buttons, gbc );
        
        stuff.setBorder( BorderFactory.createEmptyBorder( 10, 10, 10, 10 ) );

        JScrollPane scroll = new JScrollPane( stuff );
        scroll.setBorder( null );
        
        setLayout( new BorderLayout() );
        add( scroll, BorderLayout.CENTER );
        
        getRootPane().setDefaultButton( pubButton );
        
        setModal( true );
    }
    

    
    @Override
    public void setVisible( boolean b )
    {
        pack();
        center();
        
        super.setVisible( b );
    }

    private void center()
    {
        Dimension size = getSize();
          
        Rectangle dad = parent.getBounds();
    
        int x = dad.x + ( dad.width - size.width ) / 2;
        int y = dad.y + ( dad.height - size.height ) / 2;
    
        setLocation( x, y );
    }
    
    
    public int getMode()
    {
        return mode;
    }

   
    @Override
    public void actionPerformed( ActionEvent e )
    {
        if(e.getSource() == pubButton)
        	mode = 0;
        if(e.getSource() == subButton)
        	mode = 1;
        if(e.getSource() == fmButton)
        	mode = 2;
        	     
        dispose();
    }
}